/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.report.controller;

import com.tamnhat.lazyLoading.LazyAbstractDataModel;
import com.tamnhat.report.entity.BaocaoPro;
import com.tamnhat.report.entity.BaocaoTonghopMT;
import com.tamnhat.report.entity.Doitac;
import com.tamnhat.report.entity.Sanpham;
import com.telsoft.applet.util.DateUtil;
import com.telsoft.applet.util.StringUtil;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;
import telsoft.admin.util.AppServer;
import telsoft.cms_admin.login.LoginBean;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
@ManagedBean
@ViewScoped
public class BaocaoTonghopMTController {

    private Date tuNgay;
    private Date denNgay;
    private int partner_id;
    private int product_id;
    private List<BaocaoTonghopMT> list = new ArrayList<BaocaoTonghopMT>();
    private ArrayList<Doitac> listDoitac = new ArrayList<Doitac>();
    private ArrayList<Sanpham> listDichvu = new ArrayList<Sanpham>();
    private boolean checkAdmin = false;
    private LazyDataModel<BaocaoTonghopMT> lazyDataModel;

    /**
     * Creates a new instance of BaocaoTonghopMTController
     */
    public BaocaoTonghopMTController() {
        Long j = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.USER_ID).toString());
        Connection cn = null;
        try {
            listDoitac = BaocaoPro.listDoitac(j);
            cn = AppServer.getConnection();
            checkAdmin = BaocaoPro.checkAdmin(j,cn);            
            listDichvu = BaocaoPro.listSanpham(j);
        } catch (Exception e) {
            Database.closeObject(cn);
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error: "+e.getMessage()));            
        }
    }

    public LazyDataModel<BaocaoTonghopMT> getLazyDataModel() {
        return lazyDataModel;
    }

    public void setLazyDataModel(LazyDataModel<BaocaoTonghopMT> lazyDataModel) {
        this.lazyDataModel = lazyDataModel;
    }
    
    public boolean isCheckAdmin() {
        return checkAdmin;
    }

    public void setCheckAdmin(boolean checkAdmin) {
        this.checkAdmin = checkAdmin;
    }

    public ArrayList<Doitac> getListDoitac() {
        return listDoitac;
    }

    public void setListDoitac(ArrayList<Doitac> listDoitac) {
        this.listDoitac = listDoitac;
    }

    public ArrayList<Sanpham> getListDichvu() {
        return listDichvu;
    }

    public void setListDichvu(ArrayList<Sanpham> listDichvu) {
        this.listDichvu = listDichvu;
    }

    public List<BaocaoTonghopMT> getList() {
        return list;
    }

    public void setList(List<BaocaoTonghopMT> list) {
        this.list = list;
    }

    public Date getTuNgay() {
        return tuNgay;
    }

    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    public Date getDenNgay() {
        return denNgay;
    }

    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    public int getPartner_id() {
        return partner_id;
    }

    public void setPartner_id(int partner_id) {
        this.partner_id = partner_id;
    }

    public int getProduct_id() {
        return product_id;
    }

    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }

    public boolean checkInput() {
        if (tuNgay.after(denNgay)) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Từ ngày không thể lớn hơn đến ngày"));
            return false;
        }
        return true;
    }

    public String thongke() {
        if (checkInput()) {
            try {
                list = BaocaoTonghopMT.getList(StringUtil.format(tuNgay, "dd-MM-yyyy"), StringUtil.format(denNgay, "dd-MM-yyyy"), partner_id, product_id);                
                lazyDataModel = new LazyAbstractDataModel<BaocaoTonghopMT>(list);
                int i = lazyDataModel.getRowCount();
                if(i > 0){
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có "+i+" bản ghi được tìm thấy!"));
                }
                else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
            } catch (Exception e) {
                e.printStackTrace();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            }
        }
        return "";
    }
}
